package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


import server.FirewallException;
import server.FirewallSession;
import server.ReadWriteException;

/**
 * The main window pannel.
 */
public class UserPanel extends JPanel 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -625782735034331445L;

	/**
	 * server controller
	 */
	private FirewallSession UI;
	
	final int BUTTON_LENGTH = 150;
	final int BUTTON_WIDTH = 70;	
	
	public UserPanel(FirewallSession ui)
	{	
		setLayout(new FlowLayout());
		UI = ui;

		try {
			Settings.load();
		} catch (IOException e) {
			/* It's ok - default values */
		} catch (ClassNotFoundException e) {
			System.out.println("Class problem in reading");
			e.printStackTrace();
		}
		addPicture();
		addStandartButtons();
		addMenuButtons();
	}

	/**
	 * 
	 */
	private void addPicture()
	{
		//D:\Technion\Jane\OS Project\GUI\
		File f = new File("fw.jpg");
//		if(f.exists())
//			System.out.println("HEY!!!!!!");
//		
		JPanel picture = new JPanel();		
		ImageIcon image = new ImageIcon("fw.jpg");
	
		JLabel background = new JLabel(image);

		background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
		picture.add(background);
		picture.setVisible(true);

/*		JPanel panel = new JPanel();
		panel.setOpaque(false);
		panel.add( new JButton( "Hello" ) );
		MainFrame.getInstance().setContentPane( panel );*/

	    add(picture, BorderLayout.EAST);
	}

	/**
	 * 
	 */
	private void addStandartButtons() 
	{
		Box tableBox = Box.createHorizontalBox();
		
		JButton finish = new JButton("Finish");
		finish.setToolTipText("Close and save changes");
		JButton cancel = new JButton("Cancel");
		cancel.setToolTipText("Close without saving changes");
		
		tableBox.add(finish);
		tableBox.add(cancel);
		
		setLayout(new BorderLayout());    
		
	    
		finish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try {
					Settings.save();
				} catch (IOException e) {
					System.out.println("Output file isn't defined");
				}
				System.gc();
				System.runFinalization();
				System.exit(0);				
			}	  
		});
		  
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				System.exit(0);				
			}	  
		});

	    add(tableBox, BorderLayout.SOUTH);
	}

	/**
	 * Adds Regular Expression, Rules, Logger, Error Reports, Help windows call buttons.
	 */
	private void addMenuButtons() 
	{
		JPanel menu = new JPanel(new GridLayout(7, 1));

		/* Regular Expression window call button and its listener */
		JButton pass = new JButton("Regular Expression");
		menu.add(pass);
		pass.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				RegExpFrame frame = RegExpFrame.getInstance(UI);
				frame.setVisible(true);
			}	  
		});
		
		/* Rules window call button and its listener */
		JButton rules = new JButton("Rules");
		menu.add(rules);
		rules.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				AbstractRulesFrame frame = null;
				
				/* Call the appropriate rules window */
				if(Settings.userMode.equals(UserMode.standartUser))
				{
					frame = RulesStandartFrame.getInctance(UI);
				}
				else
				{
					frame = RulesAdvancedFrame.getInctance(UI);				
				}	

				frame.setVisible(true);
			}	  
		});
		
		/* Logger window call button and its listener */
		JButton logger = new JButton("Logger");
		menu.add(logger);
		logger.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				LoggerFrame frame = LoggerFrame.getInctance(UI);
				frame.setVisible(true);
			}	  
		});		
	
		/* Error window call button and its listener */
		JButton error = new JButton("Exceptions List");
		menu.add(error);
		error.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				/* No need to create the frame - created in initialization */
				ErrorFrame.currentErrorFrame.setVisible(true);
			}	  
		});			
		
		/* Help window call button and its listener */
		JButton help = new JButton("Help");
		menu.add(help);
		help.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				/* No need to create the frame - created in initialization */
				HelpFrame.currentHelpFrame.setVisible(true);
			}	  
		});		
		
		/* FW on/off button */
		final JButton onoff = new JButton("FW Off");
		onoff.setToolTipText("Turn off the FW");
		menu.add(onoff);	
		onoff.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try {
						
					/* Turn FW on/off according to the current status */
					if(Settings.fwTurnedOn == true)
					{
						UI.turnOff();
						Settings.fwTurnedOn = false;
						onoff.setText("FW On");
						onoff.setToolTipText("Turn on the FW");
					}
					else
					{
						UI.turnOn();
						Settings.fwTurnedOn = true;
						onoff.setText("FW Off");
						onoff.setToolTipText("Turn on the FW");
					}
				} catch (RemoteException e) {
					ErrorFrame.AddError("Failed to turn on/off the firewall:" + e.getMessage(), new JFrame());
				} catch (FirewallException e) {
					ErrorFrame.AddError("Failed to turn on/off the firewall:" + e.getMessage(), new JFrame());
				} catch (ReadWriteException e) {
					ErrorFrame.AddError("Failed to turn on/off the firewall:" + e.getMessage(), new JFrame());
				}
			}	  
		});
	
	    add(menu, BorderLayout.WEST);
	}
}
